package com.ww.cheappliers.consumer.impl;

import java.util.concurrent.atomic.AtomicInteger;

import org.apache.log4j.Logger;
import org.springframework.util.Assert;

import com.ww.cheappliers.Message;
import com.ww.cheappliers.consumer.AbstractConsumer;
import com.ww.cheappliers.receive.MessageReaper;

public class LogConsumerImpl extends AbstractConsumer {
	private static final Logger log = Logger.getLogger(LogConsumerImpl.class);
	// 收到消息计数
	private static final AtomicInteger msgCounter = new AtomicInteger();

	public void gotMessage(MessageReaper reaper, Message msg) {
		Assert.notNull(msg);
		
		StringBuffer msgDetail = new StringBuffer();
		if(msg.msgs != null && msg.msgs.length > 0)
		for(String s : msg.msgs)
			msgDetail.append("\n[").append(s + "").append("]");
		
		log.info("Message Count: " + msgCounter.incrementAndGet()
				+ ", Receive: "
				+ (msgDetail.length() > 0 ? msgDetail : "NULL"));
	}

	public static int getReceviedMsgCount(){
		return msgCounter.get();
	}
}
